package com.xuelang.pipeline.infrastructure.resource.repository;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.resource.dto.CloudEnvironmentQuery;
import com.xuelang.pipeline.domain.resource.CloudEnvironment;
import com.xuelang.pipeline.domain.resource.repository.CloudEnvironmentRepository;
import com.xuelang.pipeline.infrastructure.resource.converter.CloudEnvironmentToCloudEnvironmentDOConverter;
import com.xuelang.pipeline.infrastructure.resource.dataobject.CloudEnvironmentDO;
import com.xuelang.pipeline.infrastructure.resource.mapper.CloudEnvironmentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 多云环境仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class CloudEnvironmentRepositoryImpl implements CloudEnvironmentRepository {

    @Autowired
    CloudEnvironmentMapper cloudEnvironmentMapper;

    @Override
    public PageRecord<CloudEnvironment> pageList(long currentPage, long pageSize, CloudEnvironmentQuery cloudEnvironmentQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<CloudEnvironmentDO> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<CloudEnvironmentDO> wrapper = Wrappers.lambdaQuery(CloudEnvironmentDO.class);
        if (cloudEnvironmentQuery != null) {
            if (cloudEnvironmentQuery.getId() != null) {
                wrapper.eq(CloudEnvironmentDO::getId, cloudEnvironmentQuery.getId());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getName())) {
                wrapper.eq(CloudEnvironmentDO::getName, cloudEnvironmentQuery.getName());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getAlias())) {
                wrapper.eq(CloudEnvironmentDO::getAlias, cloudEnvironmentQuery.getAlias());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getDescription())) {
                wrapper.eq(CloudEnvironmentDO::getDescription, cloudEnvironmentQuery.getDescription());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getBuilderId())) {
                wrapper.eq(CloudEnvironmentDO::getBuilderId, cloudEnvironmentQuery.getBuilderId());
            }
        }
        cloudEnvironmentMapper.selectPage(page, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords().stream().map(CloudEnvironmentToCloudEnvironmentDOConverter.INSTANCE::cloudEnvironmentDOToCloudEnvironment).collect(Collectors.toList())
        );
    }

    @Override
    public List<CloudEnvironment> list(CloudEnvironmentQuery cloudEnvironmentQuery) {
        LambdaQueryWrapper<CloudEnvironmentDO> wrapper = Wrappers.lambdaQuery(CloudEnvironmentDO.class);
        if (cloudEnvironmentQuery != null) {
            if (cloudEnvironmentQuery.getId() != null) {
                wrapper.eq(CloudEnvironmentDO::getId, cloudEnvironmentQuery.getId());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getName())) {
                wrapper.eq(CloudEnvironmentDO::getName, cloudEnvironmentQuery.getName());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getAlias())) {
                wrapper.eq(CloudEnvironmentDO::getAlias, cloudEnvironmentQuery.getAlias());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getDescription())) {
                wrapper.eq(CloudEnvironmentDO::getDescription, cloudEnvironmentQuery.getDescription());
            }
            if (StringUtils.isNotBlank(cloudEnvironmentQuery.getBuilderId())) {
                wrapper.eq(CloudEnvironmentDO::getBuilderId, cloudEnvironmentQuery.getBuilderId());
            }
        }
        return cloudEnvironmentMapper.selectList(wrapper).stream()
                .map(CloudEnvironmentToCloudEnvironmentDOConverter.INSTANCE::cloudEnvironmentDOToCloudEnvironment).collect(Collectors.toList());
    }


    @Override
    public Boolean save(CloudEnvironment cloudEnvironment) {
        return cloudEnvironmentMapper.insert(CloudEnvironmentToCloudEnvironmentDOConverter.INSTANCE.cloudEnvironmentToCloudEnvironmentDO(cloudEnvironment)) == 1;
    }

    @Override
    public Boolean update(CloudEnvironment cloudEnvironment) {
        return cloudEnvironmentMapper.update(
                CloudEnvironmentToCloudEnvironmentDOConverter.INSTANCE.cloudEnvironmentToCloudEnvironmentDO(cloudEnvironment),
                Wrappers.lambdaQuery(CloudEnvironmentDO.class).eq(CloudEnvironmentDO::getId, cloudEnvironment.getId())) == 1;
    }

    @Override
    public Boolean delete(Long id) {
        return cloudEnvironmentMapper.delete(Wrappers.lambdaQuery(CloudEnvironmentDO.class).eq(CloudEnvironmentDO::getId, id)) == 1;
    }

    @Override
    public Boolean existsName(Long id, String name) {
        List<CloudEnvironmentDO> cloudEnvironmentDOS = cloudEnvironmentMapper.selectList(Wrappers.lambdaQuery(CloudEnvironmentDO.class).eq(CloudEnvironmentDO::getName, name));
        if (ObjectUtil.isEmpty(cloudEnvironmentDOS)) {
            return false;
        }
        return cloudEnvironmentDOS.stream().anyMatch(cloudEnvironmentDO -> !ObjectUtil.equals(id, cloudEnvironmentDO.getId()));
    }

    @Override
    public Boolean existsAlias(Long id, String alias) {
        List<CloudEnvironmentDO> cloudEnvironmentDOS = cloudEnvironmentMapper.selectList(Wrappers.lambdaQuery(CloudEnvironmentDO.class).eq(CloudEnvironmentDO::getAlias, alias));
        if (ObjectUtil.isEmpty(cloudEnvironmentDOS)) {
            return false;
        }
        return cloudEnvironmentDOS.stream().anyMatch(cloudEnvironmentDO -> !ObjectUtil.equals(id, cloudEnvironmentDO.getId()));
    }

    @Override
    public CloudEnvironment query(Long id) {
        CloudEnvironmentDO cloudEnvironmentDO = cloudEnvironmentMapper.selectOne(Wrappers.lambdaQuery(CloudEnvironmentDO.class).eq(CloudEnvironmentDO::getId, id));
        return CloudEnvironmentToCloudEnvironmentDOConverter.INSTANCE.cloudEnvironmentDOToCloudEnvironment(cloudEnvironmentDO);
    }
}
